<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('insurance_base_adjustments', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 调整基本信息
            $table->string('adjustment_number', 50)->unique(); // 调整编号
            $table->string('adjustment_type', 50); // base_adjustment, policy_change, new_employee, etc.
            $table->string('adjustment_reason', 200); // 社保基数从1310调整为1353

            // 调整范围
            $table->string('scope_type', 50); // all, department, position, individual
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id')->nullable();
            $table->string('position_name', 100)->nullable();
            $table->json('affected_employees')->nullable(); // 受影响员工列表

            // 保险类型
            $table->unsignedBigInteger('insurance_type_id');
            $table->string('insurance_type_name', 100);

            // 调整前后的数值
            $table->decimal('previous_base_amount', 10, 2); // 调整前基数
            $table->decimal('new_base_amount', 10, 2); // 调整后基数
            $table->decimal('adjustment_percentage', 5, 2); // 调整百分比
            $table->decimal('adjustment_amount', 10, 2); // 调整金额

            // 缴费变化
            $table->decimal('previous_employee_contribution', 10, 2)->nullable();
            $table->decimal('new_employee_contribution', 10, 2)->nullable();
            $table->decimal('previous_company_contribution', 10, 2)->nullable();
            $table->decimal('new_company_contribution', 10, 2)->nullable();

            // 调整时间
            $table->date('effective_date'); // 生效日期
            $table->date('adjustment_date'); // 调整操作日期

            // 追溯设置
            $table->boolean('retroactive')->default(false); // 是否追溯调整
            $table->date('retroactive_from')->nullable(); // 追溯起始日期
            $table->date('retroactive_to')->nullable(); // 追溯结束日期

            // 审批信息
            $table->string('approval_status', 20)->default('pending'); // pending, approved, rejected
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->string('approved_by_name', 100)->nullable();
            $table->datetime('approved_at')->nullable();
            $table->text('approval_notes')->nullable();

            // 执行状态
            $table->string('execution_status', 20)->default('pending'); // pending, processing, completed, failed
            $table->integer('affected_records')->default(0); // 受影响记录数
            $table->text('execution_result')->nullable(); // 执行结果

            // 财务影响
            $table->decimal('total_cost_increase', 12, 2)->default(0); // 总成本增加
            $table->decimal('monthly_cost_increase', 10, 2)->default(0); // 月度成本增加

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['adjustment_type', 'approval_status']);
            $table->index(['scope_type']);
            $table->index(['insurance_type_id']);
            $table->index(['effective_date']);
            $table->index(['adjustment_date']);
            $table->index(['approval_status']);
            $table->index(['execution_status']);
            $table->index(['department_id']);
            $table->index(['position_id']);
            $table->index(['adjustment_number']);

            // 外键约束
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
            $table->foreign('insurance_type_id')->references('id')->on('insurance_types')->onDelete('cascade');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('insurance_base_adjustments');
    }
};
